

	package com.teamtwoseven.todo;

	import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Locale;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

	import com.google.android.maps.GeoPoint;
	import com.google.android.maps.MapActivity;
	import com.google.android.maps.MapController;
	import com.google.android.maps.MapView;
	import com.google.android.maps.Overlay;
	import com.google.android.maps.ItemizedOverlay;
	import com.google.android.maps.OverlayItem;


import android.app.AlertDialog;
import android.content.DialogInterface;
	import android.content.Intent;
	import android.graphics.drawable.Drawable;
import android.location.Address;
import android.location.Geocoder;
	import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;

	public class NewLocation extends MapActivity{
		
		private MapView map;
		private String locationSearchText;
		private Drawable drawable;
		private int x, y;
		private List<Overlay> overlayList;
		private MapController mc;
		private GeoPoint point;
		
	    /** Called when the activity is first created. */
	    @Override
	    public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.map_layout);
	                
	        map = (MapView) findViewById(R.id.mvMap_View);
	        map.setBuiltInZoomControls(true);
	        mc = map.getController();
	        overlayList = map.getOverlays();
	        drawable = getResources().getDrawable(R.drawable.locationmarker);

	        //get data that was passed to the activity from NewItem 
	        Intent myIntent = getIntent();
	        
	        point = getGeoPoint(getLocationInfo(myIntent.getExtras().getString("location text").replaceAll(" ","%20")));
			OverlayItem overlayItem = new OverlayItem(point, "one", "two");
			ThisItemizedOverlay customOverlay = new ThisItemizedOverlay(drawable, NewLocation.this);
			customOverlay.addOverlayItem(overlayItem);
			overlayList.add(customOverlay);
			mc.animateTo(point);
			mc.setZoom(15);
			//map.invalidate();
			

	    }
	    
	    
		@Override
		public void onBackPressed() {
			// TODO Auto-generated method stub
			//startActivity(new Intent("com.teamtwoseven.todo.MAIN"));
			finish();
		}


		@Override
		protected boolean isRouteDisplayed() {
			// TODO Auto-generated method stub
			return false;
		}
		
		public static JSONObject getLocationInfo(String address) {

			HttpGet httpGet = new HttpGet("http://maps.google."
					+ "com/maps/api/geocode/json?address=" + address
					+ "ka&sensor=false");
			HttpClient client = new DefaultHttpClient();
			HttpResponse response;
			StringBuilder stringBuilder = new StringBuilder();

			try {
				response = client.execute(httpGet);
				HttpEntity entity = response.getEntity();
				InputStream stream = entity.getContent();
				int b;
				while ((b = stream.read()) != -1) {
					stringBuilder.append((char) b);
				}
			} catch (ClientProtocolException e) {
			} catch (IOException e) {
			}

			JSONObject jsonObject = new JSONObject();
			try {
				jsonObject = new JSONObject(stringBuilder.toString());
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			return jsonObject;
		}
		
		public static GeoPoint getGeoPoint(JSONObject jsonObject) {

			Double lon = new Double(0);
			Double lat = new Double(0);

			try {

				lon = ((JSONArray)jsonObject.get("results")).getJSONObject(0)
					.getJSONObject("geometry").getJSONObject("location")
					.getDouble("lng");

				lat = ((JSONArray)jsonObject.get("results")).getJSONObject(0)
					.getJSONObject("geometry").getJSONObject("location")
					.getDouble("lat");

			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			return new GeoPoint((int) (lat * 1E6), (int) (lon * 1E6));

		}
}
